﻿#include "imageView.h"


imageView::imageView(QWidget* parent)
	:QLabel(parent)
{
	m_strLable = "NULL";
	setParent(parent);
}


//QPainter画图
void imageView::paintEvent(QPaintEvent* event)
{
	QPainter painter(this);
	painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
	// 根据窗口计算应该显示的图片的大小


	m_Mutex.lock();
	int width = qMin(m_Image.width(), this->width());
	int height = width * 1.0 / (m_Image.width() * 1.0 / m_Image.height());
	height = qMin(height, this->height());
	width = height * 1.0 * (m_Image.width() * 1.0 / m_Image.height());

	// 平移
	painter.translate(this->width() / 2 + XPtInterval, this->height() / 2 + YPtInterval);

	// 缩放
	painter.scale(ZoomValue, ZoomValue);

	// 绘制图像
	QRect picRect(-width / 2, -height / 2, width, height);
	painter.drawImage(picRect, m_Image);


	// 绘制标签

	// 重新设置原点
	painter.translate(-(this->width() / 2) + 1 ,- (this->height() / 2) + 20);

	painter.setPen(QColor(255, 10, 10));
	QFont font("Arial", 10); // 设置字体
	painter.setFont(font);

	// 显示文本的背景矩形
	QRect strRect = painter.fontMetrics().boundingRect(m_strLable);
	painter.setBrush(QBrush(QColor(204, 202, 202)));
	painter.setPen(QColor(204, 202, 202));
	painter.drawRect(strRect);

	// 显示标签
	painter.setPen(QColor(50, 50, 50));
	painter.drawText( 0,  0, m_strLable);

	m_Mutex.unlock();
}


//鼠标滚轮滚动
void imageView::wheelEvent(QWheelEvent* event)
{
	int value = event->delta();
	if (value > 0)  //放大
		OnZoomInImage();
	else            //缩小
		OnZoomOutImage();

	update();
}


//鼠标摁下
void imageView::mousePressEvent(QMouseEvent* event)
{
	OldPos = event->pos();
	Pressed = true;
}



void imageView::mouseDoubleClickEvent(QMouseEvent* event)
{
	if (event->button() == Qt::LeftButton)
	{
		ZoomValue = 1.0;
		XPtInterval = 0;
		YPtInterval = 0;
		update();
	}
}


//鼠标松开
void imageView::mouseMoveEvent(QMouseEvent* event)
{
	if (!Pressed)
		return QWidget::mouseMoveEvent(event);

	this->setCursor(Qt::SizeAllCursor);
	QPoint pos = event->pos();
	int xPtInterval = pos.x() - OldPos.x();
	int yPtInterval = pos.y() - OldPos.y();

	XPtInterval += xPtInterval;
	YPtInterval += yPtInterval;

	OldPos = pos;
	update();
}


//鼠标发射事件
void imageView::mouseReleaseEvent(QMouseEvent* event)
{
	Pressed = false;
	setCursor(Qt::ArrowCursor);
}


//图片放大
void imageView::OnZoomInImage()
{
	ZoomValue += 1;
	if (ZoomValue > 35)
	{
		ZoomValue = 35;
	}
	update();
}


//图片缩小
void imageView::OnZoomOutImage()
{
	ZoomValue -= 1;
	if (ZoomValue <= 0)
	{
		ZoomValue = 0.5;
		return;
	}

	update();
}


//图片还原
void imageView::OnPresetImage()
{
	ZoomValue = 1.0;
	XPtInterval = 0;
	YPtInterval = 0;
	update();
}



void imageView::setImage(QImage image)
{
	//this->setScaledContents(true);//设置label允许缩放属性
	//image.scaled(this->size(),Qt::KeepAspectRatio, Qt::SmoothTransformation);//把图片缩放到label窗口适应的大小
	m_Mutex.lock();
	m_Image = image.copy(0, 0, image.width(), image.height());
	m_Mutex.unlock();
	update();
}


void imageView::setLable(QString lable)
{
	m_strLable = lable;
}



QImage imageView::getImage()
{
	return m_Image;
}



void imageView::clear()
{
	m_Mutex.lock();
	QPixmap pixmap = QPixmap("");
	m_Image = pixmap.toImage();
	m_Mutex.unlock();
	update();
}

